home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2108 / 2108.xpi / content / common.js < prev    next >
Text File  |  2009-08-18  |  5KB  |  123 lines

  1. var stylishCommon = {
  2.  
  3.     XULNS: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  4.  
  5.     //compares CSS, taking into account platform differences
  6.     cssAreEqual: function(css1, css2) {
  7.         if (css1 == null && css2 == null) {
  8.             return true;
  9.         }
  10.         if (css1 == null || css2 == null) {
  11.             return false;
  12.         }
  13.         return css1.replace(/\s/g, "") == css2.replace(/\s/g, "");
  14.     },
  15.  
  16.     domApplyAttributes: function(element, json) {
  17.         for (var i in json)
  18.             element.setAttribute(i, json[i]);
  19.     },
  20.  
  21.     dispatchEvent: function(doc, type) {
  22.         if (!doc) {
  23.             return;
  24.         }
  25.         var stylishEvent = doc.createEvent("Events");
  26.         stylishEvent.initEvent(type, false, false, doc.defaultView, null);
  27.         doc.dispatchEvent(stylishEvent);
  28.     },
  29.  
  30.     getAppName: function() {
  31.         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
  32.         return appInfo.name;
  33.     },
  34.  
  35.     deleteWithPrompt: function(style) {
  36.         const STRINGS = document.getElementById("stylish-common-strings");
  37.         var title = STRINGS.getString("deleteStyleTitle")
  38.         var prompt = STRINGS.getFormattedString("deleteStyle", [style.name])
  39.         var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  40.         if (prompts.confirmEx(window, title, prompt, prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_CANCEL, STRINGS.getString("deleteStyleOK"), null, null, null, {})) {
  41.             return false;
  42.         }
  43.         style.delete();
  44.         return true;
  45.     },
  46.  
  47.     fixXHR: function(request) {
  48.         //only a problem on 1.9 toolkit
  49.         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
  50.         var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);
  51.         if (versionChecker.compare(appInfo.version, "1.9") >= 0 && versionChecker.compare(appInfo.version, "1.9.3a1pre") <= 0) {
  52.             //https://bugzilla.mozilla.org/show_bug.cgi?id=437174
  53.             var ds = Components.classes["@mozilla.org/webshell;1"].createInstance(Components.interfaces.nsIDocShellTreeItem).QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  54.             ds.itemType = Components.interfaces.nsIDocShellTreeItem.typeContent;
  55.             request.channel.loadGroup = ds.getInterface(Components.interfaces.nsILoadGroup);
  56.             request.channel.loadFlags |= Components.interfaces.nsIChannel.LOAD_DOCUMENT_URI;
  57.         }
  58.     },
  59.  
  60.     getWindowName: function(prefix, id) {
  61.         return (prefix + (id || Math.random())).replace(/\W/g, "");
  62.     },
  63.  
  64.     clearAllMenuItems: function(event) {
  65.         var popup = event.target;
  66.         for (var i = popup.childNodes.length - 1; i >= 0; i--) {
  67.             popup.removeChild(popup.childNodes[i]);
  68.         }
  69.     },
  70.  
  71.     focusWindow: function(name) {
  72.         //if a window is already open, openDialog will clobber the changes made. check for an open window for this style and focus to it
  73.         var windowsMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  74.         var win = windowsMediator.getMostRecentWindow(name);
  75.         if (win) {
  76.             win.focus();
  77.             return true;
  78.         }
  79.         return false;
  80.     },
  81.  
  82.     openEdit: function(name, params) {
  83.         if (stylishCommon.focusWindow(name)) {
  84.             return;
  85.         }
  86.         params.windowType = name;
  87.         return openDialog("chrome://stylish/content/edit.xul", name, "chrome,resizable,dialog=no,centerscreen", params);        
  88.     },
  89.  
  90.     openEditForStyle: function(style) {
  91.         return stylishCommon.openEdit(stylishCommon.getWindowName("stylishEdit", style.id), {style: style});
  92.     },
  93.  
  94.     openEditForId: function(id) {
  95.         var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle);
  96.         var style = service.find(id, service.REGISTER_STYLE_ON_CHANGE | service.CALCULATE_META);
  97.         return stylishCommon.openEditForStyle(style);
  98.     },
  99.  
  100.     openInstall: function(params) {
  101.         function fillName(prefix) {
  102.             params.windowType = stylishCommon.getWindowName(prefix, params.triggeringDocument ? stylishCommon.cleanURI(params.triggeringDocument.location.href) : null);
  103.         }
  104.         if (Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBoolPref("extensions.stylish.editOnInstall")) {
  105.             fillName("stylishEdit");
  106.             stylishCommon.openEdit(params.windowType, params);
  107.         } else {
  108.             fillName("stylishInstall");
  109.             if (!stylishCommon.focusWindow(params.windowType)) {
  110.                 openDialog("chrome://stylish/content/install.xul", params.windowType, "chrome,resizable,dialog=no,centerscreen,resizable", params);
  111.             }
  112.         }
  113.     },
  114.  
  115.     cleanURI: function(uri) {
  116.         var hash = uri.indexOf("#");
  117.         if (hash > -1) {
  118.             uri = uri.substring(0, hash);
  119.         }
  120.         return uri;
  121.     }
  122. }
  123.